<?php
include_once('common/header.php');			
if($AppUI->id <=0 || !isset($_SERVER['HTTP_REFERER'])) {
	Vfriend_Function::redirect(HOST_VFRIEND);
}
$strLink		= isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
if(!Vfriend_Function::isValidLink($strLink)) {
	Vfriend_Function::redirect(LINK_NEW);
}
$intUserId 			= Vfriend_Function::base64D($objRequest->getParam('puserid', 0));
$strRefer 			= $objRequest->getParam('prefer', LINK_NEWS);
$intNewsId 			= $objRequest->getParam('pnewsid', 0);
$strName			= $objRequest->getParam('pname', '');
$intSubCategoryId	= $objRequest->getParam('pcategoryid', 0);
$oFile				= $_FILES['pimage'];
$strContent			= $objRequest->getParam('pcontent','');
$strTags			= $objRequest->getParam('ptags','');
$strImageOld		= $objRequest->getParam('pimage_old', '');
$strSrcLink			= $objRequest->getParam('psrc_link', '');

$strImage			= '';
$intError			= 0;
if($intUserId != $AppUI->id) {
	$intError = 13;
}elseif($intSubCategoryId == 0) $intError = 10;
elseif($strName == '') $intError = 1;
elseif($strName != '' && strlen($strName) < 5) $intError = 2;
elseif(!isset($oFile['name']) && $strImageOld == '') $intError = 11;
elseif(strip_tags($strContent) == '') $intError = 5;
elseif(strip_tags($strContent) != '' && strlen(strip_tags($strContent))<20) $intError = 6;

if($intError == 0 && isset($oFile['name']) && $oFile['name'] != '') {
	$arrImage = Globals::uploadNews($oFile);	
	if($arrImage['error'] == 0) {
		$strImage = $arrImage['path'];
	} else {
		$intError = 3;
	}
}

if($intError == 0) {
	if(isset($_SESSION['sNewsError'])) unset($_SESSION['sNewsError']);
	if(isset($_SESSION['sNewsEdit'])) unset($_SESSION['sNewsEdit']);
	$strShort 	= Vfriend_Function::wordCut($strContent);
	$objNews	= new News();
	if($strImage != '') {
		$objNews->deleteImg($intNewsId);
	}			
	$intCategoryId 	= 0;
	$intError		= $objNews->update($AppUI->id, $intNewsId, $strImage, $strName, Vfriend_Function::stripTags($strContent,5), $strShort, $strTags, $intCategoryId, $intSubCategoryId, $strSrcLink);
	if($intError == 0) {
		Vfriend_Function::setMsg($arrMessage[2]);
		Vfriend_Function::redirect($strRefer);
	} else {
		$intError = 8;
	}
}
if($intError > 0) {
	$objNews	= new News();
	$row		= $objNews->get($intNewsId);
	$_SESSION['sNewsError']	= $arrError[$intError];
	$_SESSION['sNewsEdit']	= $objRequest->getParams();
	Vfriend_Function::redirect(Vfriend_Function::linkEditNews($intNewsId, $row['name']));
}
?>
